﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sustainalytics.Reporting.Framework;
using Foxit.PDF.PageElements;
using Foxit.PDF;

namespace Sustainalytics.Governance.Reporting
{
    public class PdfReportSectionGenerator : IPdfReportSectionGenerator
    {
        public PdfReport _report;
        public PdfReportSection _reportSection;
        public List<OutlinePosition> _outlinePositions;
        public List<PageElementPosition> _pageElementPositions;
        public ReportDataSource _dataSource;

        public const float EmptyRowHeight = 10f;

        public PdfReportSection GeneratePdfReportSection(object objectDataSource)
        {
            _dataSource = objectDataSource as ReportDataSource;

            if (_dataSource == null || _dataSource.CompanyId == null)
            {
                throw new ArgumentException("ReportDataSource does not contain the necessary data for report generation!");
            }

            InitializeReportSection();

            ComposeSectionContent();

            if (_report.Pages.Any())
                _reportSection.PdfDocument = _report.Draw();

            return _reportSection;
        }

        public void InitializeReportSection()
        {
            _report = new PdfReport();

            _outlinePositions = new List<OutlinePosition>();
            _pageElementPositions = new List<PageElementPosition>();

            _reportSection = new PdfReportSection
            {
                Id = Guid.NewGuid(),
                Name = "Sources Section",
                PdfDocument = new byte[0],
                LastUpdate = DateTime.UtcNow,
                OutlinePositions = _outlinePositions,
                PageElementPositions = _pageElementPositions
            };
        }

        public virtual void ComposeSectionContent()
        {
        }

        public static Table GetDefaultTable(float left, float top, float width, float height)
        {
            var table = new Table(left, top, width, height);
            table.Border.Width = 0.0f;
            table.Border.Color = Grayscale.Black;
            table.Border.LineStyle = LineStyle.None;
            table.BackgroundColor = RgbColor.White;
            table.CellSpacing = 0.0f;
            table.CellDefault.Align = TextAlign.Left;
            table.CellDefault.VAlign = VAlign.Top;
            table.CellDefault.Padding.Value = 0.0f;
            table.CellDefault.Border.Width = 0.0f;
            table.CellDefault.Border.LineStyle = LineStyle.None;
            table.CellDefault.Border.Color = RgbColor.White;

            return table;
        }
    }
}
